<?php
$globalNavs = (new \app\models\Nav())->getListByCondition('id, pid, title, navurl, listorder,linktype', '1=1');
$globalNavs = app\libs\Util::listToTree($globalNavs);

function makeNav($navs) {
    $ret = '<a href="/" class="x-nav-item x-nav-logo">LOGO</a>';

    foreach($navs as $nav){
        if(isset($nav['_child'])) {
            $sub = '';
            foreach($nav['_child'] as $child){
                $sub .= '<a target="'. ('2' === $child['linktype'] ? '_blank' : 'self')  .'" class="x-nav-drop-item" href="'. $child['navurl'] .'">'. $child['title'] .'</a>';
            }
            $ret .= '<div class="x-nav-item x-nav-drop-wrapper">' .
                '<a target="'. ('2' === $nav['linktype'] ? '_blank' : 'self')  .'" href="'. $nav['navurl'] .'">'. $nav['title'] .'</a>' .
                '<div class="x-nav-drop" style="left: 0; right: auto">'. $sub .'</div>' .
            '</div>';

        } else {
            $ret .= '<a target="'. ('2' === $nav['linktype'] ? '_blank' : 'self')  .'" class="x-nav-item" href="'. $nav['navurl'] .'">'. $nav['title'] .'</a>';
        }
    }

    $form = '<div class="x-nav-user" id="navuser"></div>'
        . '<form action="/search" method="get" target="_blank" class="x-nav-search-wrapper">'
            . '<input class="x-nav-search-key" name="k" maxlength="10" autocomplete="off" placeholder="搜一下" />'
            . '<a href="javascript:;"><i class="x-nav-search-icon x-icon-search"></i></a>'
        . '</form>';

    return $ret . $form;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title><?php echo \Fate::$app->iniReader->get('seo', 'title'); if('' !== $this->title){echo ' - ' . $this->title;}?></title>
<meta name="keywords" content="<?php echo \Fate::$app->iniReader->get('seo', 'keywords'); ?>" />
<meta name="description" content="<?php if(isset($this->description)){echo ' - ' . $this->description;}else{echo \Fate::$app->iniReader->get('seo', 'description');}?>" />
<meta name="author" content="<?php echo \Fate::$app->iniReader->get('seo', 'author'); ?>" />
<meta name="robots" content="all" />
<meta name="copyright" content="<?php echo \Fate::$app->iniReader->get('seo', 'copyright'); ?>" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />

<link href="<?php echo RES; ?>/pc/css/common.css" rel="stylesheet" />
<script type="text/javascript">
var Fate = {
"BASE": "<?php echo \fate\http\Request::getInstance()->getHostInfo(); ?>",
"STATIC": "<?php echo RES; ?>",
"PRETTY_ROUTER": "<?php echo \Fate::$app->iniReader->get('system', 'pretty_router'); ?>",
"UP": "",
"COOKIE_PREFIX": "<?php echo \Fate::$app->cookiePrefix; ?>"
};
</script>

<script type="text/javascript" src="<?php echo RES; ?>/loader.js"></script>
<script type="text/javascript" src="<?php echo RES; ?>/xui.js"></script>
<script type="text/javascript" src="<?php echo RES; ?>/pcall.js"></script>
<script>
loader.config({
baseUrl: Fate.STATIC,
debug: false
});
</script>
<script type="text/javascript" src="/sdk/usersdk.js"></script>

<?php echo $this->getHeadAssets(); ?>
<style type="text/css" rel="stylesheet">
.x-indexnav-container,
.x-nav-wrapper-index,
.x-nav-wrapper {
background-repeat: no-repeat;
background-position: center bottom;
background-image: url("/dist/pc/images/themes/nav.jpg");
}
</style>
</head>
<body>
<!-- nav -->
<?php
if(isset($this->useIndexLayout)) {
?>
<nav class="x-indexnav-container">
    <div class="x-nav-wrapper-index" id="navwrapper">
    <div class="x-nav x-nav-index">
        <?php echo makeNav($globalNavs); ?>
    </div>
    </div>
</nav>
<script>
var globalNavWrapper = document.getElementById('navwrapper');
var globalNavNode = globalNavWrapper.querySelector('.x-nav');
var globalNavTop = globalNavWrapper.offsetTop;
window.onscroll = function() {
var scrollTop = document.documentElement.scrollTop;

if(scrollTop > globalNavTop) {
    globalNavWrapper.style.position = 'fixed';
    globalNavWrapper.style.top = 0;
    globalNavNode.style.boxShadow = 'none';

} else {
    globalNavWrapper.removeAttribute('style');
    globalNavNode.removeAttribute('style');
}
};
</script>
<?php } else { ?>
<div class="x-nav-wrapper">
    <div class="x-nav">
        <?php echo makeNav($globalNavs); ?>
    </div>
</div>
<?php } ?>
<!-- // nav -->

<?php echo $this->contentHtml; ?>

<?php
if(!isset($this->disableFooter)) {
    $allCate = (new \app\models\Category())->getListByCondition('id, pid, name', 'pid>0');
?>
<div class="x-footer">
    <div class="x-wrapper">
        <div class="x-footer-column x-pullleft">
            <h3>关于</h3>
            <?php echo \Fate::$app->iniReader->get('seo', 'footer'); ?>
        </div>
        <div class="x-footer-column x-pullleft">
            <h5 >导航</h5>
            <ul>
                <li><a target="_blank" href="<?php echo \app\libs\Util::U('feed'); ?>">订阅</a></li>
            </ul>
            <div>
<?php foreach($allCate as $cate){
    echo '<a target="_blank" href="'. \app\libs\Util::U('category', ['id' => $cate['id']]) .'">'. $cate['name'] .'</a>';
} ?>
            </div>
        </div>
        <div class="x-footer-column x-pullleft">
            <h5>其他</h5>
            <div>本站由<a rel="nofollow" target="_blank" href="https://gitee.com/tailipu/fateblog_php">FateBlog</a>驱动</div>
        </div>
        <div class="x-clear"></div>
    </div>
    <div class="x-footer-copyright">&copy;Since 2018</div>
</div>
<?php } ?>

<script>USER_SDK.getInstance().initNav('navuser');</script>
</body>
</html>
